//
//  WBHomeController.swift
//  WeiBo
//
//  Created by 安中 on 2020/4/28.
//  Copyright © 2020 az. All rights reserved.
//

import UIKit

//定义全局常量，尽量使用private修饰
private let cellId = "cellId"


class WBHomeController: WBBaseController {

    //微博数据数组
    private lazy var dataSource = [String]()
    //加载数据
    override func loadData() {
        print("开始加载数据")
        //模拟延时加载数据
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            for i in 0..<20 {
                //如果是上拉加载，将数据追加到底部
                if self.isPullUp {
                    self.dataSource.append("上拉 \(i)")
                } else {
                    //下拉刷新，将数据插入数据数组顶部
                    self.dataSource.insert(i.description, at: 0)
                }
            }
            print("刷新列表")
            //结束刷新控件
            self.refresh?.endRefreshing()
            //恢复上拉加载标记
            self.isPullUp = false
            //刷新列表
            self.tableView?.reloadData()
        }
    }
    
//    override func viewDidLoad() {
//        super.viewDidLoad()
//
//        BJDLog(AppConstants.AppKey.UMeng)
//    }
    
    @objc func showFriends() {
        print(#function)
        let vc = WBDemoController()
//        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension WBHomeController {
    override func setupUI() {
        super.setupUI()
        view.backgroundColor = UIColor.cyan
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}

//MARK: - 表格数据源方法
extension WBHomeController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = dataSource[indexPath.row]
        return cell
    }
}
